package pl.edu.mimuw.irs.rcp.controller.command;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;

import pl.edu.mimuw.irs.rcp.controller.editor.workflow.handler.AbstractCommandStackSelectionHandler;
import pl.edu.mimuw.irs.rcp.persistence.PersistenceManager;

public abstract class AbstractPersistenceSelectionHandler extends AbstractCommandStackSelectionHandler {

	// *** Attributes

	// *** Methods

	public abstract Object execute(ExecutionEvent event, CommandStack stack,
			PersistenceManager manager, Object[] selection);
	
	@Override
	protected Object execute(ExecutionEvent event, CommandStack stack,
			Object[] selection) throws ExecutionException {
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
		if (editor == null) return null;
		PersistenceManager manager = null;
		try {
			manager = PersistenceManager.getInstance(editor.getEditorInput());
		} catch (CoreException e) {
			throw new ExecutionException("Exception during PersistenceManager retrieval.", e);
		}
		if (manager == null) return null;
		
		return execute(event, stack, manager, selection);
	}

}
